package com.itheima.util;

import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class DataUtils {
    /**
     * 生成随机整数数组
     * @param count
     * @param start
     * @param end
     * @return
     */
    public static int[] generateRandomArray(int count, int start, int end) {
        int[] arr = new int[count];
        for (int i = 0; i < count; i++) {
            arr[i] = (int) (Math.random() * (end - start) + start);
        }
        return arr;
    }

    public static List<Integer> randomIntegers(int count, int rangeStart, int rangeEnd) {
        return randomIntegers(count, rangeStart, rangeEnd, i -> true, false);
    }
    public static List<Integer> randomIntegers(int count, int rangeStart, int rangeEnd, boolean shuffle) {
        return randomIntegers(count, rangeStart, rangeEnd, i -> true, shuffle);
    }
    public static List<Integer> randomIntegers(int count, int rangeStart, int rangeEnd, Predicate<Integer> filter) {
        return randomIntegers(count, rangeStart, rangeEnd, filter, false);
    }

    public static List<Integer> randomIntegers(int count, int rangeStart, int rangeEnd,
                                           Predicate<Integer> filter, boolean shuffle) {
        List<Integer> rst = IntStream.rangeClosed(rangeStart, rangeEnd)
                .boxed()
                .filter(filter)
                .collect(Collectors.toList());

        if (shuffle) {
            Collections.shuffle(rst);
        }

        return rst.subList(0, count);
    }
}
